<?php

namespace backend\controllers;

use Yii;
use common\models\customelements\CustomElements;
use common\models\customelements\CustomElementsSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use common\models\customset\CustomSet;

/**
 * CustomElementsController implements the CRUD actions for CustomElements model.
 */
class CustomElementsController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all CustomElements models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new CustomElementsSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single CustomElements model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new CustomElements model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new CustomElements();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing CustomElements model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing CustomElements model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the CustomElements model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return CustomElements the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = CustomElements::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

    /**
     * 自定义表单
     */
    public function actionCustomerform()
    {
        $id = 1;
        $set_info = CustomSet::find()->where(['set_id' => $id])->asArray()->one();

        if (Yii::$app->request->post()) {
            var_dump(Yii::$app->request->post());die;

            $form_config = array();
            //$form_config["limit_time"] = (int) $_POST["limit_time"];

            // if ($_POST["limit_time"] == 1) {
            //     $form_config["startdate"] = strtotime($_POST["startdate"]);
            //     $form_config["enddate"] = strtotime($_POST["enddate"]);
            // }
            // else {
            //     $form_config["startdate"] = 0;
            //     $form_config["enddate"] = 0;
            // }
            if(!$_POST['datebefore'])
            {
                $form_config["datebefore"] = 0;
            }
            else{
                $form_config["datebefore"] = (int) $_POST["datebefore"];
            }
            if(!$_POST['datelast'])
            {
                $form_config["datelast"] = 0;
            }
            else{
                $form_config["datelast"] = $_POST["datelast"];
            }
            // $form_config["limit_nums_total"] = (int) $_POST["limit_nums_total"];
            // $form_config["limit_once_total"] = (int) $_POST["limit_once_total"];
            // $form_config["limit_nums"] = (int) $_POST["limit_nums"];
            // $form_config["limit_once"] = (int) $_POST["limit_once"];

            // if ($_POST["limit_nums_total"] == 1) {
            //     $form_config["putnums_total"] = (int) $_POST["putnums_total"];
            // }
            // else {
            //     $form_config["putnums_total"] = 0;
            // }

            // if ($_POST["limit_once_total"] == 1) {
            //     $form_config["day_nums_total"] = (int) $_POST["day_nums_total"];
            // }
            // else {
            //     $form_config["day_nums_total"] = 0;
            // }

            // if ($_POST["limit_nums"] == 1) {
            //     $form_config["putnums"] = (int) $_POST["putnums"];
            // }
            // else {
            //     $form_config["putnums"] = 0;
            // }

            // if ($_POST["limit_once"] == 1) {
            //     $form_config["day_nums"] = (int) $_POST["day_nums"];
            // }
            // else {
            //     $form_config["day_nums"] = 0;
            // }

            // $notice_config = array();
            // $notice_config["open_notice"] = (int) $_POST["open_notice"];

            // if ($_POST["open_notice"] == 1) {
            //     $notice_config["email_notice"] = (int) $_POST["email_notice"];
            //     $notice_config["open_notice_sms"] = (int) $_POST["open_notice_sms"];
            //     $notice_config["open_notice_weixin"] = (int) $_POST["open_notice_weixin"];
            //     if ($_POST["open_notice_weixin"] == 1) {
            //         $notice_config["notice_weixin"] = trim($_POST["notice_weixin"]);
            //     }
            //     else
            //     {
            //         $notice_config["notice_weixin"] = "";
            //     }
            //     if ($_POST["email_notice"] == 1) {
            //         $notice_config["email_adds"] = trim($_POST["email_adds"]);
            //         $notice_config["email_name"] = trim($_POST["email_name"]);
            //         $notice_config["public_email"] = trim($_POST["public_email"]);
            //         $notice_config["public_password"] = trim($_POST["public_password"]);
            //     }
            //     else {
            //         $notice_config["email_adds"] = "";
            //         $notice_config["email_name"] = "";
            //         $notice_config["public_email"] = "";
            //         $notice_config["public_password"] = "";
            //     }

            //     if ($_POST["open_notice_sms"] == 1) {
            //         $notice_config["notice_phone"] = trim($_POST["notice_phone"]);
            //         $notice_config["notice_phone_once"] = (int) $_POST["notice_phone_once"];
            //     }
            //     else {
            //         $notice_config["notice_phone"] = "";
            //         $notice_config["notice_phone_once"] = 0;
            //     }
            // }
            // else {
            //     $notice_config["open_notice"] = 0;
            //     $notice_config["email_notice"] = 0;
            //     $notice_config["open_notice_sms"] = 0;
            //     $notice_config["open_notice_weixin"] = 0;
            //     $notice_config["email_adds"] = "";
            //     $notice_config["email_name"] = "";
            //     $notice_config["public_email"] = "";
            //     $notice_config["public_password"] = "";
            //     $notice_config["notice_phone"] = "";
            //     $notice_config["notice_phone"] = "";
            //     $notice_config["notice_phone_once"] = 0;
            // }

            // $pay_config = array();
            // $pay_config["pay_notice_more"] = (int) $_POST["pay_notice_more"];
            // $pay_config["pay_notice"] = (int) $_POST["pay_notice"];
            // $limit = array("form_config" => serialize($form_config), "notice_config" => serialize($notice_config), "pay_config" => serialize($pay_config));
            // $_POST["price"] = (string) $_POST["price"] * 100;
            $component = json_decode($_POST["componentJsonStr"], true);

            var_dump($component);die;
            foreach ($component["form"]["component"] as $key => $value ) {
                $insert[$key] = $value;
                $insert[$key]["numtype"] = $value["numtype"];
                $insert[$key]["datetype"] = ($value["datetype"] ? $value["datetype"] : "d");
                $insert[$key]["layout"] = $value["layout"];
                $insert[$key]["imgnum"] = $value["imgnum"];
                $insert[$key]["numbertype"] = $value["numbertype"];
                $insert[$key]["img"] = $value["img"];
                $insert[$key]["imglink"] = $value["imglink"];
                $insert[$key]["alignstyle"] = $value["alignstyle"];
                $insert[$key]["titlealignstyle"] = $value["titlealignstyle"];
                $insert[$key]["pictureshow"] = $value["pictureshow"];
                $insert[$key]["subtitle"] = $value["subtitle"];
                $insert[$key]["snumber"] = ($value["snumber"] ? $value["snumber"] : 9);
                $insert[$key]["stype"] = ($value["stype"] ? $value["stype"] : 9);
                $insert[$key]["evalue"] = ($value["value"] ? serialize($value["value"]) : "");
                $insert[$key]["token"] = trim($_POST["token"]);
                $insert[$key]["sort"] = $key;
            }

            $componentStyleJsonStr = json_decode($_POST["componentStyleJsonStr"], true);
            $base_config = array();
            $base_config["first_banner"] = $_POST["first_banner"];
            $base_config["first_banner_url"] = trim($_POST["first_banner_url"]);
            $base_config["second_banner"] = $_POST["second_banner"];
            $base_config["second_banner_url"] = trim($_POST["second_banner_url"]);
            $base_config["third_banner"] = $_POST["third_banner"];
            $base_config["third_banner_url"] = trim($_POST["third_banner_url"]);
            $base_config["first_icon"] = $_POST["first_icon"];
            $base_config["second_icon"] = $_POST["second_icon"];
            $base_config["third_icon"] = $_POST["third_icon"];
            $base_config["fourth_icon"] = $_POST["fourth_icon"];
            $base_config["title1"] = trim($_POST["title1"]);
            $base_config["title2"] = trim($_POST["title2"]);
            $base_config["footbtn_title"] = trim($_POST["footbtn_title"]);

            foreach ($componentStyleJsonStr as $key => $value ) {
                $base_config["bodybgcolor"] = $componentStyleJsonStr["bodybgcolor"];
                $base_config["title1_config"] = (!empty($componentStyleJsonStr["title1"]) ? serialize($componentStyleJsonStr["title1"]) : "");
                $base_config["title2_config"] = (!empty($componentStyleJsonStr["title2"]) ? serialize($componentStyleJsonStr["title2"]) : "");
                $base_config["baseinfo_config"] = (!empty($componentStyleJsonStr["baseinfo"]) ? serialize($componentStyleJsonStr["baseinfo"]) : "");
                $base_config["hdtit_config"] = (!empty($componentStyleJsonStr["hdtit"]) ? serialize($componentStyleJsonStr["hdtit"]) : "");
                $base_config["hddesc_config"] = (!empty($componentStyleJsonStr["hddesc"]) ? serialize($componentStyleJsonStr["hddesc"]) : "");
                $base_config["optionvalue_config"] = (!empty($componentStyleJsonStr["optionvalue"]) ? serialize($componentStyleJsonStr["optionvalue"]) : "");
                $base_config["footbtn_config"] = (!empty($componentStyleJsonStr["footbtn"]) ? serialize($componentStyleJsonStr["footbtn"]) : "");
                $base_config["conbg"] = $componentStyleJsonStr["conbg"];
            }

            if ($set_info) {
                unset($_POST["is_pay"]);
                unset($_POST["pay_name"]);
                unset($_POST["price"]);
                //$_POST["detail"] = $this->_post("detail", "trim");
                //$this->set_db->where($where)->save($_POST);
                //$custom_elements_all = M("custom_elements")->where(array("set_id" => $set_info["set_id"], "token" => $this->token))->field("eid")->select();

                // foreach ($custom_elements_all as $k => $v ) {
                //     $eid[$v["eid"]] = $v["eid"];
                // }
                var_dump($insert);die;
                foreach ($insert as $key => $value ) {
                    if (M("custom_elements")->where(array("set_id" => $set_info["set_id"], "token" => $this->token, "eid" => $value["eid"]))->find()) {
                        unset($eid[$value["eid"]]);
                        M("custom_elements")->where(array("set_id" => $set_info["set_id"], "token" => $this->token, "eid" => $value["eid"]))->save($value);
                    }
                    else {
                        M("custom_elements")->where(array("set_id" => $set_info["set_id"], "token" => $this->token, "eid" => $value["eid"]))->delete();
                        $value["set_id"] = $set_info["set_id"];
                        $value["token"] = $this->token;
                        M("custom_elements")->add($value);
                    }
                }

                $eids = implode(array_keys($eid), ",");

                if (!empty($eids)) {
                    M("custom_elements")->where(array(
                        "set_id" => $set_info["set_id"],
                        "token"  => $this->token,
                        "eid"    => array("in", $eids)
                        ))->delete();
                }

                if (M("custom_base")->where(array("set_id" => $set_info["set_id"], "token" => $this->token))->find()) {
                    M("custom_base")->where(array("set_id" => $set_info["set_id"], "token" => $this->token))->save($base_config);
                }
                else {
                    $base_config["set_id"] = $set_info["set_id"];
                    $base_config["token"] = $this->token;
                    M("custom_base")->add($base_config);
                }

                $this->limit_db->where(array("limit_id" => $set_info["limit_id"]))->save($limit);
                $this->handleKeyword($this->_post("set_id", "intval"), "Custom", $this->_post("keyword", "trim"), 1);
                $this->success("修改成功", U("Custom/set", array("token" => $this->token, "set_id" => $this->_get("set_id", "intval"))));
            }
            else {
                $limit_id = $this->limit_db->add($limit);
                $_POST["addtime"] = time();
                $_POST["limit_id"] = $limit_id;

                if ($this->set_db->create()) {
                    $_POST["detail"] = $this->_post("detail", "trim");
                    $_POST["versions"] = 1;
                    $id = $this->set_db->add($_POST);

                    foreach ($insert as $key => $value ) {
                        $insert[$key]["set_id"] = $id;
                    }

                    M("custom_elements")->addAll($insert);
                    $base_config["set_id"] = $id;
                    $base_config["token"] = $this->token;
                    M("custom_base")->add($base_config);
                    $this->handleKeyword($id, "Custom", $this->_post("keyword", "trim"), 1);
                    $this->success("添加成功", U("Custom/index", array("token" => $this->token)));
                }
                else {
                    $this->error($this->set_db->getError());
                }
            }
        }
        else {

            if (!empty($set_info)) {

                $custom_elements = CustomElements::find()->where(["set_id" => $set_info["set_id"]])->orderBy(['sort' => SORT_ASC, 'id' => SORT_ASC])->asArray()->all();

                foreach ($custom_elements as $key => $value) {
                    if ($value["evalue"]) {
                        $custom_elements[$key]["value"] = unserialize($value["evalue"]);
                        unset($custom_elements[$key]["evalue"]);
                    }
                }
            }


            return $this->renderPartial('customerform', [
                'set' => $set_info,
            ]);
        }
    }
    /**
     * 设置表单
     */

    public function actionSet()
    {
        $set_info = CustomSet::find()->where(['set_id' => $id])->asArray()->one();
        $model = new CustomElements();
        if (Yii::$app->request->post()) {

            $_POST["succ_info"] = (empty($_POST["succ_info"]) ? "提交成功" : $this->_post("succ_info", "trim"));
            $_POST["err_info"] = (empty($_POST["succ_info"]) ? "提交失败" : $this->_post("err_info", "trim"));
            $form_config = array();
            $form_config["limit_time"] = (int) $_POST["limit_time"];

            if ($_POST["limit_time"] == 1) {
                $form_config["startdate"] = strtotime($_POST["startdate"]);
                $form_config["enddate"] = strtotime($_POST["enddate"]);
            }
            else {
                $form_config["startdate"] = 0;
                $form_config["enddate"] = 0;
            }
            if(!$_POST['datebefore'])
            {
                $form_config["datebefore"] = 0;
            }
            else{
                $form_config["datebefore"] = (int) $_POST["datebefore"];
            }
            if(!$_POST['datelast'])
            {
                $form_config["datelast"] = 0;
            }
            else{
                $form_config["datelast"] = $_POST["datelast"];
            }
            // $form_config["limit_nums_total"] = (int) $_POST["limit_nums_total"];
            // $form_config["limit_once_total"] = (int) $_POST["limit_once_total"];
            // $form_config["limit_nums"] = (int) $_POST["limit_nums"];
            // $form_config["limit_once"] = (int) $_POST["limit_once"];

            // if ($_POST["limit_nums_total"] == 1) {
            //     $form_config["putnums_total"] = (int) $_POST["putnums_total"];
            // }
            // else {
            //     $form_config["putnums_total"] = 0;
            // }

            // if ($_POST["limit_once_total"] == 1) {
            //     $form_config["day_nums_total"] = (int) $_POST["day_nums_total"];
            // }
            // else {
            //     $form_config["day_nums_total"] = 0;
            // }

            // if ($_POST["limit_nums"] == 1) {
            //     $form_config["putnums"] = (int) $_POST["putnums"];
            // }
            // else {
            //     $form_config["putnums"] = 0;
            // }

            // if ($_POST["limit_once"] == 1) {
            //     $form_config["day_nums"] = (int) $_POST["day_nums"];
            // }
            // else {
            //     $form_config["day_nums"] = 0;
            // }

            $notice_config = array();
            $notice_config["open_notice"] = (int) $_POST["open_notice"];

            if ($_POST["open_notice"] == 1) {
                $notice_config["email_notice"] = (int) $_POST["email_notice"];
                $notice_config["open_notice_sms"] = (int) $_POST["open_notice_sms"];
                $notice_config["open_notice_weixin"] = (int) $_POST["open_notice_weixin"];
                if ($_POST["open_notice_weixin"] == 1) {
                    $notice_config["notice_weixin"] = trim($_POST["notice_weixin"]);
                }
                else
                {
                    $notice_config["notice_weixin"] = "";
                }
                if ($_POST["email_notice"] == 1) {
                    $notice_config["email_adds"] = trim($_POST["email_adds"]);
                    $notice_config["email_name"] = trim($_POST["email_name"]);
                    $notice_config["public_email"] = trim($_POST["public_email"]);
                    $notice_config["public_password"] = trim($_POST["public_password"]);
                }
                else {
                    $notice_config["email_adds"] = "";
                    $notice_config["email_name"] = "";
                    $notice_config["public_email"] = "";
                    $notice_config["public_password"] = "";
                }

                if ($_POST["open_notice_sms"] == 1) {
                    $notice_config["notice_phone"] = trim($_POST["notice_phone"]);
                    $notice_config["notice_phone_once"] = (int) $_POST["notice_phone_once"];
                }
                else {
                    $notice_config["notice_phone"] = "";
                    $notice_config["notice_phone_once"] = 0;
                }
            }
            else {
                $notice_config["open_notice"] = 0;
                $notice_config["email_notice"] = 0;
                $notice_config["open_notice_sms"] = 0;
                $notice_config["open_notice_weixin"] = 0;
                $notice_config["email_adds"] = "";
                $notice_config["email_name"] = "";
                $notice_config["public_email"] = "";
                $notice_config["public_password"] = "";
                $notice_config["notice_phone"] = "";
                $notice_config["notice_phone"] = "";
                $notice_config["notice_phone_once"] = 0;
            }

            $pay_config = array();
            $pay_config["pay_notice_more"] = (int) $_POST["pay_notice_more"];
            $pay_config["pay_notice"] = (int) $_POST["pay_notice"];
            $limit = array("form_config" => serialize($form_config), "notice_config" => serialize($notice_config), "pay_config" => serialize($pay_config));
            $_POST["price"] = (string) $_POST["price"] * 100;
            $component = json_decode($_POST["componentJsonStr"], true);

            foreach ($component["form"]["component"] as $key => $value ) {
                $insert[$key] = $value;
                $insert[$key]["numtype"] = $value["numtype"];
                $insert[$key]["datetype"] = ($value["datetype"] ? $value["datetype"] : "d");
                $insert[$key]["layout"] = $value["layout"];
                $insert[$key]["imgnum"] = $value["imgnum"];
                $insert[$key]["numbertype"] = $value["numbertype"];
                $insert[$key]["img"] = $value["img"];
                $insert[$key]["imglink"] = $value["imglink"];
                $insert[$key]["alignstyle"] = $value["alignstyle"];
                $insert[$key]["titlealignstyle"] = $value["titlealignstyle"];
                $insert[$key]["pictureshow"] = $value["pictureshow"];
                $insert[$key]["subtitle"] = $value["subtitle"];
                $insert[$key]["snumber"] = ($value["snumber"] ? $value["snumber"] : 9);
                $insert[$key]["stype"] = ($value["stype"] ? $value["stype"] : 9);
                $insert[$key]["evalue"] = ($value["value"] ? serialize($value["value"]) : "");
                $insert[$key]["token"] = trim($_POST["token"]);
                $insert[$key]["sort"] = $key;
            }

            $componentStyleJsonStr = json_decode($_POST["componentStyleJsonStr"], true);
            $base_config = array();
            $base_config["first_banner"] = $_POST["first_banner"];
            $base_config["first_banner_url"] = trim($_POST["first_banner_url"]);
            $base_config["second_banner"] = $_POST["second_banner"];
            $base_config["second_banner_url"] = trim($_POST["second_banner_url"]);
            $base_config["third_banner"] = $_POST["third_banner"];
            $base_config["third_banner_url"] = trim($_POST["third_banner_url"]);
            $base_config["first_icon"] = $_POST["first_icon"];
            $base_config["second_icon"] = $_POST["second_icon"];
            $base_config["third_icon"] = $_POST["third_icon"];
            $base_config["fourth_icon"] = $_POST["fourth_icon"];
            $base_config["title1"] = trim($_POST["title1"]);
            $base_config["title2"] = trim($_POST["title2"]);
            $base_config["footbtn_title"] = trim($_POST["footbtn_title"]);

            foreach ($componentStyleJsonStr as $key => $value ) {
                $base_config["bodybgcolor"] = $componentStyleJsonStr["bodybgcolor"];
                $base_config["title1_config"] = (!empty($componentStyleJsonStr["title1"]) ? serialize($componentStyleJsonStr["title1"]) : "");
                $base_config["title2_config"] = (!empty($componentStyleJsonStr["title2"]) ? serialize($componentStyleJsonStr["title2"]) : "");
                $base_config["baseinfo_config"] = (!empty($componentStyleJsonStr["baseinfo"]) ? serialize($componentStyleJsonStr["baseinfo"]) : "");
                $base_config["hdtit_config"] = (!empty($componentStyleJsonStr["hdtit"]) ? serialize($componentStyleJsonStr["hdtit"]) : "");
                $base_config["hddesc_config"] = (!empty($componentStyleJsonStr["hddesc"]) ? serialize($componentStyleJsonStr["hddesc"]) : "");
                $base_config["optionvalue_config"] = (!empty($componentStyleJsonStr["optionvalue"]) ? serialize($componentStyleJsonStr["optionvalue"]) : "");
                $base_config["footbtn_config"] = (!empty($componentStyleJsonStr["footbtn"]) ? serialize($componentStyleJsonStr["footbtn"]) : "");
                $base_config["conbg"] = $componentStyleJsonStr["conbg"];
            }

            if ($set_info) {
                unset($_POST["is_pay"]);
                unset($_POST["pay_name"]);
                unset($_POST["price"]);
                $_POST["detail"] = $this->_post("detail", "trim");
                $this->set_db->where($where)->save($_POST);
                $custom_elements_all = M("custom_elements")->where(array("set_id" => $set_info["set_id"], "token" => $this->token))->field("eid")->select();

                foreach ($custom_elements_all as $k => $v ) {
                    $eid[$v["eid"]] = $v["eid"];
                }

                foreach ($insert as $key => $value ) {
                    if (M("custom_elements")->where(array("set_id" => $set_info["set_id"], "token" => $this->token, "eid" => $value["eid"]))->find()) {
                        unset($eid[$value["eid"]]);
                        M("custom_elements")->where(array("set_id" => $set_info["set_id"], "token" => $this->token, "eid" => $value["eid"]))->save($value);
                    }
                    else {
                        M("custom_elements")->where(array("set_id" => $set_info["set_id"], "token" => $this->token, "eid" => $value["eid"]))->delete();
                        $value["set_id"] = $set_info["set_id"];
                        $value["token"] = $this->token;
                        M("custom_elements")->add($value);
                    }
                }

                $eids = implode(array_keys($eid), ",");

                if (!empty($eids)) {
                    M("custom_elements")->where(array(
                        "set_id" => $set_info["set_id"],
                        "token"  => $this->token,
                        "eid"    => array("in", $eids)
                        ))->delete();
                }

                if (M("custom_base")->where(array("set_id" => $set_info["set_id"], "token" => $this->token))->find()) {
                    M("custom_base")->where(array("set_id" => $set_info["set_id"], "token" => $this->token))->save($base_config);
                }
                else {
                    $base_config["set_id"] = $set_info["set_id"];
                    $base_config["token"] = $this->token;
                    M("custom_base")->add($base_config);
                }

                $this->limit_db->where(array("limit_id" => $set_info["limit_id"]))->save($limit);
                $this->handleKeyword($this->_post("set_id", "intval"), "Custom", $this->_post("keyword", "trim"), 1);
                $this->success("修改成功", U("Custom/set", array("token" => $this->token, "set_id" => $this->_get("set_id", "intval"))));
            }
            else {
                $limit_id = $this->limit_db->add($limit);
                $_POST["addtime"] = time();
                $_POST["limit_id"] = $limit_id;

                if ($this->set_db->create()) {
                    $_POST["detail"] = $this->_post("detail", "trim");
                    $_POST["versions"] = 1;
                    $id = $this->set_db->add($_POST);

                    foreach ($insert as $key => $value ) {
                        $insert[$key]["set_id"] = $id;
                    }

                    M("custom_elements")->addAll($insert);
                    $base_config["set_id"] = $id;
                    $base_config["token"] = $this->token;
                    M("custom_base")->add($base_config);
                    $this->handleKeyword($id, "Custom", $this->_post("keyword", "trim"), 1);
                    $this->success("添加成功", U("Custom/index", array("token" => $this->token)));
                }
                else {
                    $this->error($this->set_db->getError());
                }
            }
        }
        else {
            echo 1;die;
            if (!empty($set_info)) {
                $limit_info = $this->limit_db->where(array("limit_id" => $set_info["limit_id"]))->find();
            }

            if (!empty($set_info)) {
                $custom_elements = M("custom_elements")->where(array("set_id" => $set_info["set_id"], "token" => $this->token))->order("sort asc,id asc")->select();

                foreach ($custom_elements as $key => $value ) {
                    if ($value["evalue"]) {
                        $custom_elements[$key]["value"] = unserialize($value["evalue"]);
                        unset($custom_elements[$key]["evalue"]);
                    }
                }
            }

            $base_config = array();

            if (!empty($set_info)) {
                $custom_base = M("custom_base")->where(array("set_id" => $set_info["set_id"], "token" => $this->token))->find();
                $base_config["bodybgcolor"] = $custom_base["bodybgcolor"];
                $base_config["title1"] = unserialize($custom_base["title1_config"]);
                $base_config["title2"] = unserialize($custom_base["title2_config"]);
                $base_config["baseinfo"] = unserialize($custom_base["baseinfo_config"]);
                $base_config["hdtit"] = unserialize($custom_base["hdtit_config"]);
                $base_config["hddesc"] = unserialize($custom_base["hddesc_config"]);
                $base_config["optionvalue"] = unserialize($custom_base["optionvalue_config"]);
                $base_config["conbg"] = $custom_base["conbg"];
                $base_config["footbtn"] = unserialize($custom_base["footbtn_config"]);
            }

            $form_config = unserialize($limit_info["form_config"]);
            $notice_config = unserialize($limit_info["notice_config"]);
            $pay_config = unserialize($limit_info["pay_config"]);

            if (!empty($form_config)) {
                $set_info = array_merge($set_info, $form_config);
            }

            if (!empty($notice_config)) {
                $set_info = array_merge($set_info, $notice_config);
            }

            $now = strtotime("+1 day");
            $this->assign("now", $now);
            $set_info["price"] = $set_info["price"] / 100;
            $thisWxUser = M("Wxuser")->where(array("token" => $this->token))->find();
            $this->assign("smscount", M("Users")->where(array("id" => $thisWxUser["uid"]))->getField("smscount"));
            $this->assign("set", $set_info);
            $this->assign("custom_elements", !empty($custom_elements) ? json_encode($custom_elements) : "null");
            $this->assign("custom_base", $custom_base);
            $this->assign("base_config", !empty($base_config) ? json_encode($base_config) : "null");
            $this->display();
        }
    }
}
